
###########################################################
##### Haiti elite network project  		          			#####
##### prep family characteristics                  		#####
##### 2021 mar 03                   									#####
###########################################################

## read in pol bio data
## create cohort var
## create measures of qual and quant of pol rank
## create measures of qual and quant of mil rank
## create regime measure
## read in coup plotters list
## merge biz wtih fams
## write list of fam coup plotters


#####
## supplice pol bios
#####

bios <- read.csv("01_Data/01_Raw/03_Family/pol bio database FINAL.csv", 
                 as.is=T, skip = 1)
## name cleaning
bios$name_last <- toupper(bios$name_last)
bios$name_last <- gsub(" *$", "", bios$name_last)
bios$name_last <- gsub("^ +", "", bios$name_last)


bios <- subset(bios, select = c(name_last, birth, death, milrank:polrank, legis, exec, judic, pol_first:pol_last))

## make types
bios$mil <- ifelse(is.na(bios$milrank)==F, 1, 0)


## make pol variables
bios$exec <- ifelse(bios$exec > 0, 1, 0)
bios$pol <- bios$exec


## make time variable
bios$time <- ifelse(is.na(bios$pol_first)==F | is.na(bios$pol_last)==F | is.na(bios$mil_first)==F | 
                      is.na(bios$mil_last)==F, 1, 0)
table(bios$time[bios$legis==1 | bios$exec==1 | bios$judic==1 | bios$mil==1])
table(bios$time[bios$mil==1])


## make mil dummies for pre duv, duv, and post duv 

bios$mil_duvsap1 <- ifelse(bios$mil==1 & bios$time==1 & (bios$mil_last < 1988 | bios$mil_first > 1979), 1, 0)
bios$mil_duvsap2 <- ifelse(bios$mil==1 & bios$time==1 & (bios$mil_last < 1998 | bios$mil_first > 1969), 1, 0)

bios$pol_duvsap1 <- ifelse(bios$pol==1 & bios$time==1 & (bios$pol_last < 1988 | bios$pol_first > 1979), 1, 0)
bios$pol_duvsap2 <- ifelse(bios$pol==1 & bios$time==1 & (bios$pol_last < 1998 | bios$pol_first > 1969), 1, 0)

bios$mil_duvpre1 <- ifelse(bios$mil==1 & bios$time==1 & (bios$mil_last < 1980 | bios$mil_first > 1958), 1, 0)
bios$mil_duvpre2 <- ifelse(bios$mil==1 & bios$time==1 & (bios$mil_last < 1990 | bios$mil_first > 1948), 1, 0)

bios$pol_duvpre1 <- ifelse(bios$pol==1 & bios$time==1 & (bios$pol_last < 1980 | bios$pol_first > 1958), 1, 0)
bios$pol_duvpre2 <- ifelse(bios$pol==1 & bios$time==1 & (bios$pol_last < 1990 | bios$pol_first > 1948), 1, 0)

table(bios$pol_duvsap1, bios$pol_duvsap2)
table(bios$pol_duvsap1, bios$pol_duvpre1)


## make pol dummies for pre duv, duv, and post duv 

bios$pol_preduv <- ifelse(bios$pol==1 & bios$time==1 & (bios$pol_last < 1958 | bios$pol_first < 1938), 1, 0)
bios$pol_duv <- ifelse(bios$pol==1 & bios$time==1 & (bios$pol_last >= 1958 & bios$pol_last < 1987) |
                         (bios$pol_first >= 1938 & bios$pol_first < 1967), 1, 0)
bios$pol_postduv <- ifelse(bios$pol==1 & bios$time==1 & (bios$pol_first >= 1987 | bios$pol_last < 2007), 1, 0)
table(bios$pol_preduv, bios$pol_duv)
table(bios$pol_duv, bios$pol_postduv)

bios$mil_preduv <- ifelse(bios$mil==1 & bios$time==1 & (bios$mil_last < 1958 | bios$mil_first < 1938), 1, 0)
bios$mil_duv <- ifelse(bios$mil==1 & bios$time==1 & (bios$mil_last >= 1958 & bios$mil_last < 1987) |
                         (bios$mil_first >= 1938 & bios$mil_first < 1967), 1, 0)
bios$mil_postduv <- ifelse(bios$mil==1 & bios$time==1 & (bios$mil_first >= 1987 | bios$mil_last < 2007), 1, 0)
table(bios$mil_preduv, bios$mil_duv)
table(bios$mil_duv, bios$mil_postduv)


## cut out post_duv mil and pol elites

cutoff <- 1986

bios$drop <- ifelse(bios$mil_last >= cutoff | bios$pol_last >= cutoff | bios$birth >= (cutoff-18), 1, 0)
bios$drop <- ifelse(is.na(bios$drop)==T, 0, bios$drop)
bios <- subset(bios, bios$drop!=1)


## aggregate by family

fam <- data.table(fam)

fam <- summaryBy(legis + judic + exec + mil + pol + mil_preduv + mil_duv + mil_postduv +
                   pol_preduv + pol_duv + pol_postduv + mil_duvsap1 + pol_duvsap1 + 
                   mil_duvpre1 + pol_duvpre1 ~ name_last, 
                 FUN = sum, na.rm = T, data = bios)

colnames(fam) <- sapply(strsplit(colnames(fam), "\\."), "[[", 1)

write.csv(fam, "01_Data/02_Clean/polbios_fam.csv")


#####
## coup plotters
#####

ofac <- read.csv("01_Data/01_Raw/03_Family/treasurydepartmentlist v2.csv", as.is=T)

## split comps and ind

ofaci <- subset(ofac, ofac$type == "individual")
ofacc <- subset(ofac, ofac$type == "business")

## split individual names

ofaci$last_name <- sapply(strsplit(ofaci$treas_name, " "), "[[", 1)

ofac_fam <- data.table(ofaci)
ofac_fam <- ofac_fam[, list('coup_con' = length(treas_name)), by = 'last_name']
ofac_fam$coup_bi <- 1

write.csv(ofac_fam, "01_Data/02_Clean/coup_fams.csv")






